package com.whu.zqm.controller;

import com.whu.zqm.entity.Products;
import com.whu.zqm.entity.Supplier;
import com.whu.zqm.service.ProductsService;
import com.whu.zqm.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("supplier")
public class SupplierController {
    @Autowired
    SupplierService supplierService;

    @PostMapping("/add")
    public ResponseEntity<String> addSupplier(@RequestBody Supplier s) {
        try {
            Supplier res = supplierService.addSupplier(s);
            return ResponseEntity.ok("添加成功！" + res.getId());
        } catch (Exception e) {
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    @GetMapping("/id/{id}")
    public ResponseEntity<Supplier> getSupplier(@PathVariable long id) {
        Supplier res = supplierService.getSupplier(id);
        if (res == null) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.ok(res);
        }
    }


    @DeleteMapping("delete/{id}")
    public ResponseEntity<Void> deleteSupplier(@PathVariable long id){
        supplierService.deleteSupplier(id);
        return ResponseEntity.ok().build();
    }

}
